# LuckPay Web Manage System
#
# Copyright (c) 2016 Lucky Byte, Inc.
#
$ ->
    form = $('form.main')
    form.form
        inline: true,
        fields:
            url_back:     optional: false, rules: [ type: 'empty' ]
            url_query:    optional: false, rules: [ type: 'empty' ]
            timeout:      optional: false, rules: [
                { type: 'empty' }, { type: 'integer[5..30]' },
            ]
            chnl_type:   optional: false, rules: [ type: 'empty' ]
            v13_enc:     optional: false, rules: [ type: 'empty' ]

    # 当禁用状态时，冻结页面元素
    if form.find('button#disable').data('disabled')
        form.find('*').not('button#disable')
            .addClass('disabled').attr('disabled', 'disabled')

    # 禁用按钮事件
    form.find('button#disable').click ->
        if $(this).data('disabled')
            # 启用时无需确认
            $(this).addClass 'loading'
            return $.post '/o/071/disable',
                uuid: form.form 'get value', 'uuid'
                disabled: not $(this).data 'disabled'
        else
            if $(this).hasClass('has_clicked')
                $(this).removeClass('has_clicked').text('禁 用')
                $(this).addClass 'loading'
                $.post '/o/071/disable', {
                    uuid: form.form 'get value', 'uuid'
                    disabled: not $(this).data 'disabled'
                }, (resp) ->
                    form.find('button#disable').removeClass 'loading'
            else
                # 禁用时需要二次确认
                $(this).addClass('has_clicked').text('确认禁用')
